<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null">
      <!-- 搜索区域 -->
      <a-form layout="horizontal">
        <div :class="advanced ? null: 'fold'">
          <a-row>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="法人行"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-select
                          mode="default"
                          :allowClear="true"
                          style="width: 100%"
                          @change="handleInstChange"
                          v-model="queryParams.instId">
                  <a-select-option v-for="r in instIdData" :key="r.label">{{r.label}}-{{r.value}}</a-select-option>
                </a-select>
<!--                <a-input v-if="!this.user.admin" v-model="queryParams.instId" :value="this.user.instId"
                         readonly="readonly"/>-->
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24" v-show="queryParams.instId!=''">
              <a-form-item
                label="查询类型"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-select
                  mode="default"
                  :allowClear="true"
                  style="width: 100%"
                  show-search
                  placeholder="请选择"
                  option-filter-prop="children"
                  :filter-option="filterOption"
                  v-model="queryParams.statisType">
                  <a-select-option key="01">终端</a-select-option>
                  <a-select-option key="02">二维码</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row v-show="queryParams.instId!=''">
            <a-col :md="12" :sm="24">
              <a-form-item label="收单机构"
                           :labelCol="{span: 5}"
                           :wrapperCol="{span: 18, offset: 1}">
                <a-select
                  show-search
                  placeholder="请选择收单机构"
                  option-filter-prop="children"
                  style="width: 100%"
                  :allowClear="true"
                  @focus="selectAcqInstCode"
                  @search="selectAcqInstCode"
                  v-model="queryParams.acqInstCode"
                >
                  <a-select-option v-for="(d,index) in acqInstCodeData" :key="d.id">
                    {{ d.id }}-{{ d.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="商户号"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-input v-model="queryParams.mchntNo"/>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row v-show="queryParams.instId!=''">
            <a-col :md="12" :sm="24">
              <a-form-item
                label="无交易时间"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-select
                  mode="default"
                  :allowClear="true"
                  style="width: 100%"
                  show-search
                  placeholder="请选择"
                  option-filter-prop="children"
                  :filter-option="filterOption"
                  v-model="queryParams.dateArea">
                  <a-select-option key="3">3个月</a-select-option>
                  <a-select-option key="12">12个月</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="终端号/二维码"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-input v-model="queryParams.termNo"/>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row v-show="queryParams.instId!=''">
            <a-col :md="12" :sm="24">
              <a-form-item
                label="状态"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-select
                  mode="default"
                  :allowClear="true"
                  style="width: 100%"
                  show-search
                  placeholder="请选择"
                  option-filter-prop="children"
                  :filter-option="filterOption"
                  v-model="queryParams.status">
                  <a-select-option key="01">正式</a-select-option>
                  <a-select-option key="02">停用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
        </div>
        <div style="text-align: center" v-show="queryParams.instId!=''">
          <a-button type="primary" @click="search">查询</a-button>
          <a-button style="margin-left: 8px" @click="reset">重置</a-button>
        </div>
      </a-form>
    </div>
    <div v-show="queryParams.instId!=''">
      <!-- 表格区域 -->
      <a-table ref="TableInfo"
               :columns="columns"
               :rowKey="record => record.id"
               :dataSource="dataSource"
               :pagination="pagination"
               :loading="loading"
               :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               :scroll="{ x: 2600 }"
               @change="handleTableChange">
        <template slot="email" slot-scope="text, record">
          <a-popover placement="topLeft">
            <template slot="content">
              <div>{{text}}</div>
            </template>
            <p style="width: 150px;margin-bottom: 0">{{text}}</p>
          </a-popover>
        </template>
        <template slot="noTxnRegOperation" slot-scope="text, record">
          <a-icon v-hasPermission="['noTxnReg:stop']" type="minus-circle" theme="twoTone" twoToneColor="#4a9ff5"
                  @click="stopNoTxnReg(record)" title="停用"></a-icon>&nbsp;
          <a-icon v-hasPermission="['noTxnReg:use']" type="play-circle" theme="twoTone" twoToneColor="#4a9ff5"
                  @click="useNoTxnReg(record)" title="启用"></a-icon>&nbsp;
          <a-icon v-hasPermission="['noTxnReg:view']" type="eye" theme="twoTone" twoToneColor="#42b983"
                  @click="viewNoTxnReg(record)" title="查看"></a-icon>
          <a-badge v-hasNoPermission="['noTxnReg:edit','noTxnReg:view']" userStatus="warning" text="无权限"></a-badge>
        </template>

      </a-table>
      <!-- 查看 -->
      <no-txn-reg-info
        ref="noTxnRegInfo"
        @close="handleNoTxnRegInfoClose"
        :noTxnRegInfoVisiable="noTxnRegInfo.visiable">
      </no-txn-reg-info>
      <!-- 停用 -->
      <no-txn-reg-stop
        ref="noTxnRegStop"
        @close="handleNoTxnRegStopClose"
        @success="handleNoTxnRegStopSuccess"
        :noTxnRegStopVisiable="noTxnRegStop.visiable"
        :instId="this.queryParams.instId">
      </no-txn-reg-stop>

      <!-- 启用 -->
      <no-txn-reg-use
        ref="noTxnRegUse"
        @close="handleNoTxnRegUseClose"
        @success="handleNoTxnRegUseSuccess"
        :noTxnRegUseVisiable="noTxnRegUse.visiable"
        :instId="this.queryParams.instId">
      </no-txn-reg-use>
    </div>
  </a-card>
</template>

<script>
  import RangeDate from '@/components/datetime/RangeDate'
  import NoTxnRegInfo from './NoTxnRegInfo'
  import NoTxnRegStop from './NoTxnRegStop'
  import NoTxnRegUse from './NoTxnRegUse'
  import {mapState} from "vuex"

  export default {
    name: "NoTxnReg",
    components: {NoTxnRegInfo, RangeDate, NoTxnRegUse, NoTxnRegStop},
    data() {
      return {
        acqInstCodeData: [],
        advanced: false,
        noTxnRegInfo: {
          visiable: false,
          data: {}
        },
        noTxnRegStop: {
          visiable: false
        },
        noTxnRegUse: {
          visiable: false
        },

        queryParams: {
          instId: '',
          statisType: '',
          acqInstCode: '',
          mchntNo: '',
          dateArea: '',
          termNo: '',
          status: ''
        },
        dataSource: [],
        sortedInfo: null,
        paginationInfo: null,
        selectedRowKeys: [],
        pagination: {
          pageSizeOptions: ['10', '20', '30', '40', '100'],
          defaultCurrent: 1,
          defaultPageSize: 10,
          showQuickJumper: true,
          showSizeChanger: true,
          showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
        },
        loading: false,
        instIdData: [],
        da: []
      }
    },
    computed: {
      ...mapState({
        user: state => state.account.user,

      }),
      columns() {
        let {sortedInfo} = this
        sortedInfo = sortedInfo || {}
        let {filteredInfo} = this
        filteredInfo = filteredInfo || {}
        return [{
          title: '统计维度',
          dataIndex: 'statisType',
          scopedSlots: {customRender: 'statisType'},
        }, {
          title: '收单机构',
          dataIndex: 'acqInstCode',
          scopedSlots: {customRender: 'acqInstCode'},
        }, {
          title: '收单机构名称',
          dataIndex: 'acqInstName',
          scopedSlots: {customRender: 'acqInstName'},
        }, {
          title: '商户号',
          dataIndex: 'mchntNo',
          scopedSlots: {customRender: 'mchntNo'},
        }, {
          title: '商户名称',
          dataIndex: 'mchntName',
          scopedSlots: {customRender: 'mchntName'},
        }, {
          title: '商户类型',
          dataIndex: 'mchntType',
          customRender: (text, row, index) => {
            switch (text) {
              case '00':
                return <a-tag color = "pink" > 普通商户 < /a-tag>
              case '10':
                return <a-tag color = "pink" > 连锁商户 < /a-tag>
              case '11':
                return <a-tag color = "pink" > 连锁商户门店 < /a-tag>
              case '20':
                return <a-tag color = "pink" > 小微商户 < /a-tag>
              default:
                return text
            }
          },
          filters: [
            {text: '普通商户', value: '00'},
            {text: '连锁商户', value: '10'},
            {text: '连锁商户门店', value: '11'},
            {text: '小微商户', value: '20'}
          ],
          filterMultiple: false,
          filteredValue: filteredInfo.mchntType || null,
          onFilter: (value, record) => record.mchntType.includes(value)
        }, {
          title: '商户联系方式',
          dataIndex: 'mchntContactPhone',
          scopedSlots: {customRender: 'mchntContactPhone'},
        }, {
          title: '微信昵称',
          dataIndex: 'wechatName',
          scopedSlots: {customRender: 'wechatName'},
        }, {
          title: '终端/二维码标识',
          dataIndex: 'termNo',
          scopedSlots: {customRender: 'termNo'},
        }, {
          title: '状态',
          dataIndex: 'status',
          customRender: (text, row, index) => {
            switch (text) {
              case '00':
                return <a-tag color = "pink" > 非正式 < /a-tag>
              case '01':
                return <a-tag color = "cyan" > 正式 < /a-tag>
              case '02':
                return <a-tag color = "pink" > 停用 < /a-tag>
              case '03':
                return <a-tag color = "pink" > 已注销 < /a-tag>
              default:
                return text
            }
          },
          filters: [
            {text: '非正式', value: '00'},
            {text: '正式', value: '01'},
            {text: '停用', value: '02'},
            {text: '已注销', value: '03'}
          ],
          filterMultiple: false,
          filteredValue: filteredInfo.status || null,
          onFilter: (value, record) => record.status.includes(value)
        }, {
          title: '审核状态',
          dataIndex: 'auditStatus',
          customRender: (text, row, index) => {
            switch (text) {
              case '00':
                return <a-tag color = "cyan" > 待提交审批 < /a-tag>
              case '01':
                return <a-tag color = "pink" > 新增-审批流程中 < /a-tag>
              case '02':
                return <a-tag color = "pink" > 新增-审批通过 < /a-tag>
              case '03':
                return <a-tag color = "pink" > 新增-审批拒绝 < /a-tag>
              case '11':
                return <a-tag color = "cyan" > 注销-审批流程中 < /a-tag>
              case '12':
                return <a-tag color = "pink" > 注销-审批通过 < /a-tag>
              case '13':
                return <a-tag color = "pink" > 注销-审批拒绝 < /a-tag>
              case '21':
                return <a-tag color = "pink" > 停用-审批流程中 < /a-tag>
              case '22':
                return <a-tag color = "cyan" > 停用-审批通过 < /a-tag>
              case '23':
                return <a-tag color = "pink" > 停用-审批拒绝 < /a-tag>
              case '31':
                return <a-tag color = "pink" > 启用-审批流程中 < /a-tag>
              case '32':
                return <a-tag color = "pink" > 启用-审批通过 < /a-tag>
              case '33':
                return <a-tag color = "cyan" > 启用-审批拒绝 < /a-tag>
              case '41':
                return <a-tag color = "pink" > 修改-审批流程中 < /a-tag>
              case '42':
                return <a-tag color = "pink" > 修改-审批通过 < /a-tag>
              case '43':
                return <a-tag color = "pink" > 修改-审批拒绝 < /a-tag>
              default:
                return text
            }
          },
          filters: [
            {text: '待提交审批', value: '00'},
            {text: '新增-审批流程中', value: '01'},
            {text: '新增-审批通过', value: '02'},
            {text: '新增-审批拒绝', value: '03'},
            {text: '注销-审批流程中', value: '11'},
            {text: '注销-审批通过', value: '12'},
            {text: '注销-审批拒绝', value: '13'},
            {text: '停用-审批流程中', value: '21'},
            {text: '停用-审批通过', value: '22'},
            {text: '停用-审批拒绝', value: '23'},
            {text: '启用-审批流程中', value: '31'},
            {text: '启用-审批通过', value: '32'},
            {text: '启用-审批拒绝', value: '33'},
            {text: '修改-审批流程中', value: '41'},
            {text: '修改-审批通过', value: '42'},
            {text: '修改-审批拒绝', value: '43'}
          ],
          filterMultiple: false,
          filteredValue: filteredInfo.auditStatus || null,
          onFilter: (value, record) => record.auditStatus.includes(value)

        }, {
          title: '最终交易时间',
          dataIndex: 'endTxnTime',
          scopedSlots: {customRender: 'endTxnTime'},
        }, {
          title: '停用时间',
          dataIndex: 'stopDate',
          scopedSlots: {customRender: 'stopDate'},
        }, {
          title: '法人行',
          dataIndex: 'instId',
          scopedSlots: {customRender: 'instId'},
        }, {
          title: '操作',
          dataIndex: 'noTxnRegOperation',
          fixed: 'right',
          scopedSlots: {customRender: 'noTxnRegOperation'}
        }]
      }
    },
    mounted() {
      this.$get('admin/upmporg/getInstList').then((r) => {
        this.instIdData = r.data
      })
    },
    methods: {
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      selectAcqInstCode(val) {
        if(this.queryParams.instId) {
          this.acqInstCodeData = []
          let param = {
            q: encodeURIComponent(`&${this.queryParams.instId}`),
            pageSize:20
          }

            this.$get('/admin/merParam/UpmpOrganization/instIdOrganizationSelect2List', param).then((r) => {
              this.acqInstCodeData = r.data.results

            })

        } else {
          this.$message.warning('请先选择法人行')
        }
      },

      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },

      handleInstChange(value) {
        this.queryParams.instId = value;
        if (value != "") {
          let params = {}
          params.instId = value;
          this.$get('/admin/csmc/noTxnReg/list1', {...params}).then((r) => {
            console.log("datasource"+r.data.rows)
            this.dataSource = r.data.rows;
            this.search();
          })
        }
        this.queryParams.acqInstCode='';
      },

      handleNoTxnRegStopClose() {
        this.noTxnRegStop.visiable = false
      },
      handleNoTxnRegStopSuccess() {
        this.noTxnRegStop.visiable = false
        this.$message.success('停用成功')
        this.search()
      },

      handleNoTxnRegUseClose() {
        this.noTxnRegUse.visiable = false
      },
      handleNoTxnRegUseSuccess() {
        this.noTxnRegUse.visiable = false
        this.$message.success('启用成功')
        this.search()
      },


      viewNoTxnReg(record) {
        this.$refs.noTxnRegInfo.setNoTxnRegFormValues(record)
        this.noTxnRegInfo.visiable = true
      },
      handleNoTxnRegInfoClose() {
        this.noTxnRegInfo.visiable = false
      },
      stopNoTxnReg(record) {
        if (record.status === '02' ) {
          this.$message.warning('停用的数据不允许再停用')
        } else {
          this.$refs.noTxnRegStop.setNoTxnRegFormValues(record)
          this.noTxnRegStop.visiable = true
        }
      },
      useNoTxnReg(record) {
        if (record.status !== '02' ) {
          this.$message.warning('非停用不能启用')
        } else {
          this.$refs.noTxnRegUse.setNoTxnRegFormValues(record)
          this.noTxnRegStop.visiable = true
        }
      },


      search() {
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }

        let {sortedInfo, filteredInfo} = this
        let sortField, sortOrder
        // 获取当前列的排序和列的过滤规则
        if (sortedInfo) {
          sortField = sortedInfo.field
          sortOrder = sortedInfo.order
        }
        let params = this.queryParams

        this.fetch({
          sortField: sortField,
          sortOrder: sortOrder,
          ...params,
          ...filteredInfo
        })
      },
      reset() {
        // 取消选中
        this.selectedRowKeys = []
        // 重置分页
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }
        // 重置列排序规则
        this.sortedInfo = null
        // 重置查询参数
        this.queryParams = {}
        // // 清空时间选择
        // this.$refs.createTime.reset()
        this.fetch()
      },
      handleTableChange(pagination, filters, sorter) {
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }

        // 将这三个参数赋值给Vue data，用于后续使用
        this.paginationInfo = pagination
        this.filteredInfo = filters
        this.sortedInfo = sorter
        // this.userInfo.visiable = false

        this.fetch({
          sortField: sorter.field,
          sortOrder: sorter.order,
          ...this.queryParams,
          ...filters
        })
      },
      fetch(params = {}) {
        this.loading = true
        if (this.paginationInfo) {
          // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
          this.$refs.TableInfo.pagination.current = this.paginationInfo.current
          this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
          params.pageSize = this.paginationInfo.pageSize
          params.pageNum = this.paginationInfo.current
        } else {
          // 如果分页信息为空，则设置为默认值
          params.pageSize = this.pagination.defaultPageSize
          params.pageNum = this.pagination.defaultCurrent
        }
        this.$get('/admin/csmc/noTxnReg/list1', {
          ...params
        }).then((r) => {
          let data = r.data
          const pagination = {...this.pagination}
          pagination.total = data.total
          this.dataSource = data.rows
          this.pagination = pagination
          this.loading = false
        })
      },
    }
  }
</script>

<style lang="less" scoped>
  @import "../../../../../static/less/Common";
</style>

